﻿using System;
using System.Collections.Generic;
using System.Text;
using Manager.App_Code.IDAL;
using Manager.App_Code.DTO;
using System.Data;

namespace Manager.App_Code.DAL
{
    class NhomKhachDatPhongDAL:StandardConnectDB, INhomKhachDatPhong
    {
        public List<DTO.NhomKhachDatPhongDTO> GetlistNhomKhachDatPhong()
        {
            List<NhomKhachDatPhongDTO> listsnhomkhachdatPhong = new List<NhomKhachDatPhongDTO>();

            DataTable dt = this.DTExecuteSelect("Select * from NhomKhachDatPhong where MaNhomKhach != 1");

            foreach (DataRow dr in dt.Rows)
            {
                listsnhomkhachdatPhong.Add(ConvertObjectDataRowToNhomKhachDatPhongDTO(dr));
            }

            return listsnhomkhachdatPhong;
            
        }

        public DTO.NhomKhachDatPhongDTO GetNhomKhachByID(string MaNhomKhach)
        {
            NhomKhachDatPhongDTO nhomkhach = new NhomKhachDatPhongDTO();
            Dictionary<string, object> paramlist = new Dictionary<string, object>();
            paramlist.Add("@MaNhomKhach",MaNhomKhach);

            DataTable dt = this.DTExecuteSelect("SP_NhomKhachDatPhong_GetByID", paramlist);

            nhomkhach = ConvertObjectDataRowToNhomKhachDatPhongDTO(dt.Rows[0]);

            return nhomkhach;
            

        }

        public bool InsertNhomKhachDatPhong(DTO.NhomKhachDatPhongDTO nhomkhach)
        {

            Dictionary<string, object> paramlist = new Dictionary<string, object>();

            paramlist.Add("@TenNhomKhach", nhomkhach.TenNhomKhach);
            paramlist.Add("@NgayDen", nhomkhach.NgayDen);
            paramlist.Add("@NgayDi", nhomkhach.NgayDi);
            paramlist.Add("@GhiChu", nhomkhach.Ghichu);


            return this.ExecuteUpdate("SP_NhomKhachDatPhong_Insert", paramlist);
            
        }

        public bool DeleteNhomKhachDatPhong(DTO.NhomKhachDatPhongDTO nhomkhach)
        {
            throw new NotImplementedException();
        }

        public bool UpdateNhomKhachDatPhong(DTO.NhomKhachDatPhongDTO nhomkhach)
        {
            throw new NotImplementedException();
        }

       


        public DTO.NhomKhachDatPhongDTO ConvertObjectDataRowToNhomKhachDatPhongDTO(System.Data.DataRow dr)
        {
            if (dr != null)
            {
                NhomKhachDatPhongDTO nhomkhach = new NhomKhachDatPhongDTO();

                nhomkhach.MaNhomKhach = int.Parse(dr["MaNhomKhach"].ToString());

                nhomkhach.TenNhomKhach = dr["TenNhomKhach"].ToString();

                if (dr["NgayDen"].ToString() != "")
                    nhomkhach.NgayDen = DateTime.Parse(dr["NgayDen"].ToString());

                if (dr["NgayDi"].ToString() != "")
                    nhomkhach.NgayDi = DateTime.Parse(dr["NgayDi"].ToString());

                nhomkhach.Ghichu = dr["GhiChu"].ToString();


                return nhomkhach;
            }
            else
                return null;
            
        }


        public int GetMaxMaNhomDatPhong()
        {
            DataTable dt = this.DTExecuteSelect("SELECT MAX(MaNhomKhach) AS MaxMaNhom FROM dbo.NhomKhachDatPhong");
            string a = dt.Rows[0]["MaxMaNhom"].ToString();
            if (a != "")
                return int.Parse(a);
            return 0;
        }
    }
}
